//app.js
import request from './utils/config.js';
App({
  onLaunch: function () {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        this.globalData.code = res.code;
      }
    })
  },
  globalData: {
    userInfo: null,
    loginUserInfo:"",
    openid:"",
    code:"",
    session_key:"",
    appid: "wxc8a471bb1cf4c4a0",
    encryptedData:"",
    iv:"",
    unionid:'',
    operId:"",
    token: "",
    caseDetailData: undefined,
    ossUrlPre:'https://winhc.oss-cn-shanghai.aliyuncs.com/',
    ossImgUrL: 'https://winhc.oss-cn-shanghai.aliyuncs.com/xcx/',
    signInfo: {},
    custId: '',
    custInfo:{},
    caseTrackInfo:{},
    listRefresh:{}
  },

  //获取openid
  getGlobalOpenid: function(sussessFn){
    console.log("app.globalData.code:" + this.globalData.code);
    let _that = this;
    wx.showLoading({
      title: '加载中',
    })
    request.request_api(_that.globalData.code, (data) => {
      console.log("openid成功", data)
      _that.globalData.openid = data.data.openid;
      _that.globalData.session_key = data.data.session_key;
      //判断有无token
      let token = wx.getStorageSync('token');
      sussessFn(token);
    }, (data) => {
      wx.hideLoading();
      wx.showModal({
        title: '系统提示',
        content: '获取openid失败',
        confirmText: '确定',
        confirmColor: '#35ca14',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
          }
        }
      })
    });
  },

  //检验token值
  checkGlobalToken: function (successFn,failFn) {
    let bodyInfo = {
      loginId: wx.getStorageSync("loginId")
    }
    let _that = this;
    request.requestPutMis_api("token", bodyInfo, (res) => {
      wx.hideLoading();
      console.log("校验token成功=", res)
      if (res.errMsg == 'request:ok' && res.statusCode == 200) {
        //开始保存数据
        wx.setStorageSync("token", res.data.token);
        wx.setStorageSync("loginId", res.data.operInfoVo.loginId);
        _that.globalData.operId = res.data.operId;
        _that.globalData.loginUserInfo = res.data.operInfoVo;
       
        let flagName = wx.getStorageSync("flagName");
        // 执行("enforce"), 诉讼("lawsuit"), 市场部("lawsuitInvest")
        let department = ""; 
        let roleQuanxian = "";
        if (flagName == "enforce") {
          department = "执行跟踪";
          roleQuanxian = "ROLE_StepRecordQuery";
        } else if (flagName == "caseTrack") {
          department = "案件跟踪";
          roleQuanxian = "ROLE_CaseQuery";
        } else if (flagName == "lawsuit") {
          department = "诉讼跟踪";
          roleQuanxian = "ROLE_LawsuitStepRecordQuery";
        } else if (flagName == "lawsuitInvest") {
          department = "客户开发";
          roleQuanxian = "ROLE_CustomInfoQuery";
        } else if (flagName == "nonLitigation") {
          department = "非诉项目";
          roleQuanxian = "ROLE_ProjectQuery";
        }

        //校验下权限
        let jurisdiction = res.data.operInfoVo.roles;
        console.log('权限jurisdiction=======', jurisdiction)
        let tempList = new Array();
        if (jurisdiction.indexOf("ROLE_CustomInfoQuery") != -1) {
          tempList.push({ name: '客户开发', flagName: "lawsuitInvest", selected: false });
        }
        if (jurisdiction.indexOf("ROLE_CaseQuery") != -1) {
          tempList.push({ name: '案件跟踪', flagName: "caseTrack", selected: false });
        }
        if (jurisdiction.indexOf("ROLE_LawsuitStepRecordQuery") != -1) {
          tempList.push({ name: '诉讼跟踪', flagName: "lawsuit", selected: false });
        }
        if (jurisdiction.indexOf("ROLE_StepRecordQuery") != -1) {
          tempList.push({ name: '执行跟踪', flagName: "enforce", selected: false });
          console.log('执行权限')
        }
        if (jurisdiction.indexOf("ROLE_ProjectQuery") != -1) {
          tempList.push({ name: '非诉项目', flagName: "nonLitigation", selected: false });
          console.log('非诉权限')
        }

        if (tempList.length >0){
          //存储部门列表
          wx.setStorageSync("departmentList", JSON.stringify(tempList));
        }

        //组织返回数据
        let returnObj = {
          loginUserInfo: res.data.operInfoVo,
          flagName: flagName,
          jurisdiction: jurisdiction,
          tempList: tempList,
          roleQuanxian: roleQuanxian,
          department: department,
        };
        successFn(returnObj);
      }else{
        failFn(res);
      }
    }, (res) => {
      wx.hideLoading();
      failFn(res);
    });
  },

  //授权获取信息
  getAuthUserInfo: function (successFn, failFn) {
    wx.getSetting({
      success: res => {
        wx.getUserInfo({
          success: res => {
            successFn(res)
          },
          fail: res => {
            failFn(res)
          }
        })
      },
      fail: function () {
        wx.showModal({
          title: '系统提示',
          content: '网络错误',
          confirmText: '确定',
          confirmColor: '#35ca14',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
            }
          }
        })
      }
    })
  },

  onShow: function (ops){
    console.log("获取进入小程序的场景=", ops)
    // if (ops.scene == 1044) {
    //   console.log("获取shareTicket的值",ops.shareTicket);
    //   if (res.shareTickets) {
    //     // 获取转发详细信息
    //     wx.getShareInfo({
    //       shareTicket: res.shareTickets[0],
    //       success(res) {
    //         res.errMsg; // 错误信息
    //         res.encryptedData; // 解密后为一个 JSON 结构（openGId  群对当前小程序的唯一 ID）
    //         res.iv; // 加密算法的初始向量
    //       },
    //       fail() { },
    //       complete() { }
    //     });
    //   }
    // }
  }
  
})